package com.huike.clues.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.dto.TbClueDTO;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.clues.domain.vo.TbClueVO;
import com.huike.clues.service.TbAssignRecordService;
import com.huike.clues.service.TbClueService;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.TbClueTrackRecordService;
import com.huike.common.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 线索业务接口实现类
 *
 * @author 93238
 * @description 针对表【tb_clue(线索)】的数据库操作Service实现
 * @createDate 2023-10-12 06:35:46
 */
@Service
public class TbClueServiceImpl extends ServiceImpl<TbClueMapper, TbClue>
        implements TbClueService {


    @Autowired
    private TbClueMapper tbClueMapper;

    @Autowired
    private TbClueTrackRecordService tbClueTrackRecordService;

    @Autowired
    private TbAssignRecordService assignRecordService;

    /**
     * 新增线索
     *
     * @param tbClueDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addTbClue(TbClueDTO tbClueDTO) {

        // 新增线索表
        TbClue tbClue = BeanUtil.toBean(tbClueDTO, TbClue.class);
        boolean flag = this.save(tbClue);
        // 分配记录实体类
        TbAssignRecord assignRecord = TbAssignRecord.builder()
                .assignId(tbClueDTO.getId())
                .latest(Constants.LATEST)
                .type(Constants.RULE_TYPE_CLUE)
                .build();
        // 新增分配记录表
        assignRecordService.save(assignRecord);

        // 线索跟进记录实体
        TbClueTrackRecord tbClueTrackRecord = TbClueTrackRecord.builder()
                .clueId(tbClueDTO.getId().toString())
                .build();
        // 新增线索跟进记录表
        tbClueTrackRecordService.save(tbClueTrackRecord);
        return flag;
    }

    /**
     * 修改线索
     *
     * @param tbClueDTO
     * @return
     */
    @Override
    public boolean updateTbClue(TbClueDTO tbClueDTO) {
        TbClue tbClue = BeanUtil.toBean(tbClueDTO, TbClue.class);
        return this.updateById(tbClue);
    }

    /**
     * 根据id获取线索基础信息
     *
     * @param id
     * @return
     */
    @Override
    public TbClueVO selectTbClue(Long id) {
        return tbClueMapper.selectTbClue(id);
    }


    /**
     * 查询线索管理列表
     *
     * @param tbClueDTO
     * @return
     */
    @Override
    public List<TbClueVO> tbClueList(TbClueDTO tbClueDTO) {
        List<TbClueVO> tbClueList = tbClueMapper.selectClueList(tbClueDTO);
        return tbClueList;
    }

    /**
     * 线索转商机
     *
     * @param id
     * @return
     */
    @Override
    public int changeBusiness(Long id) {
        return 0;
    }

    /**
     * 批量分配线索管理
     *
     * @param assignmentVo
     * @return
     */
    @Override
    public int assignmentTbClue(AssignmentVo assignmentVo) {
        // 获取所有线索
        List<Long> clueIdList = Arrays.asList(assignmentVo.getIds());
        clueIdList.forEach(clueId -> {
            // 分配记录表
            // TODO:
            // 线索跟进表
        });

        return 0;
    }
}




